package com.tfjybj.itoo.exam.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.tfjybj.itoo.exam.entity.ExaminationEntity;
import com.tfjybj.itoo.exam.entity.ExamineeEntity;
import com.tfjybj.itoo.exam.entity.StudentTeachClassEntity;
import com.tfjybj.itoo.exam.entity.TemplateEntity;
import com.tfjybj.itoo.exam.model.StudentTeachClassModel;
import com.tfjybj.itoo.exam.provider.dao.StudentDao;
import com.tfjybj.itoo.exam.provider.dao.StudentTeachClassDao;
import com.tfjybj.itoo.exam.provider.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * StudentTeachClassService接口实现类
 * ${base}表
 *
 * @author 郭满亮
 * @version 2.0.0
 * @since 2.0.0 2019-01-29 15:52:00
 */
@Service("studentTeachClassService")
public class StudentTeachClassServiceImpl extends BaseServicePlusImpl<StudentTeachClassDao, StudentTeachClassEntity> implements StudentTeachClassService {
	
	//region 模板生成
    @Resource
    private StudentTeachClassDao studentTeachClassDao;

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    @Resource
    private ExaminationService examinationService;
    @Resource
    private TemplateService templateService;
    @Resource
    private ExamineeService examineeService;
    @Resource
    private StudentService studentService;
    @Resource
    private StudentDao studentDao;
    @Resource
    private StudentTeachClassService studentTeachClassService;
    /**
     * 根据teachClassId和templateId发布小练
     *
     * @param examName 考试名称
     * @param teachClassId 上课班Id
     * @param templateId 模板Id
     * @return 根据teachClassId和templateId发布小练的结果
     * @author 郭满亮
     * @since 2.0.0 2019-01-29 15:52:00
     */
    @Override
    public ItooResult savePractice(String examName, String teachClassId, String templateId) {
        // 生成考试Id
        String examinationId = IdWorker.getIdStr();
        // 获取模板信息
        TemplateEntity templateEntity = templateService.getById(templateId);
        if(templateEntity == null) {
            return ItooResult.build(ItooResult.FAIL, "查无此模版");
        }
        // 插入考試表
        ExaminationEntity examinationEntity = new ExaminationEntity();
        examinationEntity.setId(examinationId);
        examinationEntity.setCourseId(templateEntity.getCourseId());
        examinationEntity.setExamName(examName);
        examinationEntity.setScore(templateEntity.getScore().toString());
        examinationEntity.setExamClassifyId("随堂小练");
        examinationEntity.setTemplateId(templateEntity.getId());
        examinationEntity.setStartTime(new Date());
        examinationEntity.setTemplateType(Integer.parseInt(templateEntity.getOrganizingType()));
        examinationEntity.setTopicSum(templateEntity.getTotalNumber());
        examinationEntity.setState(1);
        examinationEntity.setExamType(1);
        examinationEntity.setOperator(templateEntity.getOperator());
        examinationEntity.setIsDelete(0);
        examinationService.save(examinationEntity);

        // 插入考生表

        // 查詢考生信息
        List<ExamineeEntity> examineeEntityList = studentTeachClassDao.selectStudent(teachClassId);
        if(examineeEntityList.size() == 0) {
            return ItooResult.build(ItooResult.FAIL, "此班课没有学生");
        }
        for (ExamineeEntity examineeEntity : examineeEntityList) {
            examineeEntity.setExaminationId(examinationId);
            examineeEntity.setStudentExamState(0);
            examineeEntity.setExamType(1);
            examineeEntity.setIsDelete(0);
            examineeEntity.setOperator(templateEntity.getOperator());
        }
        examineeService.saveBatch(examineeEntityList);

        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 给上课班添加学生
     *
     * @param studentCode 学生学号
     * @param teachClassId 上课班Id
     * @param operator 操作员
     * @return 添加结果
     * @author 郭满亮
     * @since  2019年2月28日10:12:07
     */
    @Override
    public ItooResult createStudent(String studentCode, String teachClassId, String operator) {
        // 根据学号查询学生id
        String studentId = studentDao.getCodeById(studentCode);
        if (studentId == null) {
            return ItooResult.build(ItooResult.FAIL,"没有此学生");
        }
        StudentTeachClassEntity studentTeachClassEntity = new StudentTeachClassEntity();
        studentTeachClassEntity.setStudentId(studentId);
        studentTeachClassEntity.setOperator(operator);
        studentTeachClassEntity.setTeachClassId(teachClassId);
        studentTeachClassService.save(studentTeachClassEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 从上课班删除学生
     *
     * @param studentId 学生id
     * @param teachClassId 上课班Id
     * @return 删除结果
     */
    @Override
    public ItooResult updataTeachClassStudent(String studentId, String teachClassId) {
        int number = studentTeachClassDao.updataTeachClassStudent(studentId,teachClassId);
        if (number > 0) {
            return ItooResult.build(ItooResult.SUCCESS,"删除成功");
        }
        return ItooResult.build(ItooResult.FAIL,"删除失败");

    }

    public List<StudentTeachClassModel> selectStudentByteachclassID(Map param){

        return studentTeachClassDao.selectStudentByteachclassID(param);
    }

}
